<?php

if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
if (!isset($_SESSION['CHK_master_account_OK']) || $_SESSION['CHK_master_account_OK'] != 1)
{
    header("Location: op.php?op=admin&fp=amountchkpass&gametype={$gametype}");
    exit();
}
if (isset($_SESSION['subaccount']))
{
    $ui->showErrorPage("你是子帐号，不能执行这个功能！");
    exit();
}
if (isset($_GET['gametype']))
{
    $gametype = intval(getvar("gametype"));
}

$chkGametype = chkGametype($gametype);
$othertext = "";

switch ($chkGametype)
{
    case 0 :
        $ui = new UI(true);
        $show3D3S = 0;
        $tbl_change = "";
        $i = 2;
        while ($i < 6)
        {
            $g_betname["27" . $i] = "连肖" . $i;
            $g_betname["36" . $i] = "连尾" . $i;
            ++$i;
        }
        unset($g_betname[27]);
        unset($g_betname[36]);
        $g_betname_3or5 = $g_betname;
        $listArr01 = array (1, 2, 3, 5, 6, 28, 30, 15, 16, 35, 47, 51, 52);
        $othertext .= "<font color=\"red\">";
        $othertext .= "&#12288;&#12288;※ 特码、正码、正码特单项限额为单一号码！";
        $othertext .= "<br>&#12288;&#12288;※ 连尾中与不中共用退水及限额！";
        $othertext .= "</font>";
        break;
    case 1 :
    case 2 :
    case 3 :
	case 4 :
        include_once( "class_3d/CheckMode_3d.php" );
        $chk = new CheckMode_3d($curruser['acl'], 1, $gametype);
        $chk->checkShow3dPage();
        unset($chk);
        $ui = new UI_3D(true);
        $ui->header();
        $tbl_change = "_" . $g_gametype_3d[$gametype]['en'];
        if (0 < $gametype && $gametype <= 15)
        {
            $g_betname_3or5 = judge_type($gametype);
            /*foreach ($g_onlyN5Arr as $key => $value)
            {
                $listArr01[] = $value . "_0";
            }*/
            foreach ($g_2face_3d as $key => $value)
            {
                $listArr01[] = $value . "_0";
            }
        }
        else if ($gametype == 20 || $gametype == 21 || $gametype == 22 || $gametype == 23 || $gametype == 24)
        {
            $g_betname_3or5 = $g_betname_5h[$gametype];
            $listArr01 = $g_2face_5h[$gametype];
        }
		elseif($gametype==25 || $gametype==26)
		{
			foreach ($_sb_Arr as $key => $value)
            {
                $g_betname_3or5[$key."_0"] = $value;
            }

			$listArr01 = $g_2face_5h;
		}
        break;
    default :
        exit("error gametype");
        break;
}
$tpl = new Template2();
$conf = new Config();
$myWidth = "820";
$masteracc = $conf->get("master_account");
$fixeddisk = intval($conf->get("fixed_disk"));
$editpawd = $conf->get("edit_password");
$chName = $g_gametype_3d[$gametype]['ch'];
if ($curruser['acl'] != 255)
{
    diemsg($curruser['account'] . "你没有权限存取！");
}
$myamount = GetAmountSetting(1, $gametype, $gametype == 0 ? "0" : "1" );
$frmSync_amuFlag = intval(getvar("sync_amu"));
if (ispost())
{
    if ($frmSync_amuFlag == 1)
    {
        $updatedt = date("Y-m-d H:i:s");
        $sql = "UPDATE `amounts{$tbl_change}` A, `amounts{$tbl_change}` B SET\r\n            A.back1 = CASE WHEN A.back1>B.back1 THEN A.back1 ELSE B.back1 END,\r\n            A.back2 = CASE WHEN A.back2>B.back2 THEN A.back2 ELSE B.back2 END,\r\n            A.back3 = CASE WHEN A.back3>B.back3 THEN A.back3 ELSE B.back3 END,\r\n            A.back4 = CASE WHEN A.back4>B.back4 THEN A.back4 ELSE B.back4 END,\r\n            A.amount1 = CASE WHEN A.amount1>B.amount1 THEN B.amount1 ELSE A.amount1 END,\r\n            A.amount2 = CASE WHEN A.amount2>B.amount2 THEN B.amount2 ELSE A.amount2 END\r\n            WHERE A.user_id>1 and A.bettype = B.bettype and A.subid = B.subid and B.user_id=1;";
        getdb($sql);
        $errall = "检查所有帐号之退水与限额完成！！时间:" . $updatedt;
        getdb("INSERT INTO `dblog` (`logs`) VALUES ('[暂放记录]检查所有帐号退水与限额资料,时间:{$updatedt},ID:" . $curruser['id'] . ",彩类:" . $g_gametype_3d[$gametype]['chs'] . "')");
        $tpl->set("err", $errall);
    }
    else if ($frmSync_amuFlag != 1)
    {
        $errall = "";
        $diskArr = $g_disk_codeUP;
        foreach ($g_betname_3or5 as $key => $value)
        {
            $backArr = array ();
            $backArr[1] = 100 - round(floatval($_POST["back{$key}1"]), 2);
            $backArr[2] = 100 - round(floatval($_POST["back{$key}2"]), 2);
            $backArr[3] = 100 - round(floatval($_POST["back{$key}3"]), 2);
            $backArr[4] = 100 - round(floatval($_POST["back{$key}4"]), 2);
            $amount1 = intval(getvar("amount1{$key}"));
            $amount2 = intval(getvar("amount2{$key}"));
            if ($gametype == 0 && array_key_exists($key, $g_bettype_ab) && $key != $g_bettype_ab[$key][1])
            {
                $amount1 = intval(getvar("amount1" . $g_bettype_ab[$key][1]));
                $amount2 = intval(getvar("amount2" . $g_bettype_ab[$key][1]));
            }
            if ($amount2 < 0)
            {
                $amount2 = 0;
            }
            if ($amount1 < 0)
            {
                $amount1 = $amount2 * 0.5;
            }
            if ($amount2 < $amount1)
            {
                $amount1 = $amount2;
            }
            $sqlTempArr = array ();
            foreach ($diskArr as $diskKey => $diskVal)
            {
                $backTemp = $backArr[$diskKey];
                if (!( $backTemp != $myamount[$key]["back{$diskKey}"] ))
                {
                    continue;
                }
                else if ($backTemp < 50 || 100 < $backTemp)
                {
                    $errall .= "<li style=\"color:#cc0000; font-weight:bold;\">{$value}&nbsp;({$diskVal} 盘)&nbsp;不合理的退水设置！</li>";
                }
                else
                {
                    $sqlTempArr[] = "back{$diskKey} = {$backTemp}";
                    $sqlTempArr[] = "updateid=" . $curruser['id'] . " ,updatedt = '" . date("Y-m-d H:i:s", systime()) . "'";
                }
            }
            if ($amount1 != $myamount[$key][1] || $amount2 != $myamount[$key][2])
            {
                $sqlTempArr[] = "amount1 = {$amount1}";
                $sqlTempArr[] = "amount2 = {$amount2}";
                $sqlTempArr[] = "updateid=" . $curruser['id'] . " ,updatedt = '" . date("Y-m-d H:i:s", systime()) . "'";
            }
            if (0 == count($sqlTempArr))
            {
                continue;
            }
            else if ($gametype == 0)
            {
                $ikey = $key;
                $subid = 0;
                if (substr(strval($key), 0, 2) == "27")
                {
                    $ikey = 27;
                    $subid = $key - 270;
                }
                else if (substr(strval($key), 0, 2) == "36")
                {
                    $ikey = 36;
                    $subid = $key - 360;	
                }
                $sql = "UPDATE amounts{$tbl_change} SET " . implode(",", $sqlTempArr) . " WHERE user_id=1 AND bettype = {$ikey} AND subid = {$subid}";
                getdb($sql);
            }
            else if ($gametype == 20 || $gametype == 21 || $gametype == 22 || $gametype == 23 || $gametype == 24)
            {
                $sql = "UPDATE amounts{$tbl_change} SET " . implode(",", $sqlTempArr) . " WHERE user_id=1 AND bettype = {$key} AND subid = 0 AND xyid = 0";
                getdb($sql);
            }
            else
            {
                $bettypeArr = explode("_", $key);
                $sql = "UPDATE amounts{$tbl_change} SET " . implode(",", $sqlTempArr) . " WHERE user_id=1 AND bettype = {$bettypeArr[0]} AND subid = {$bettypeArr[1]}";
				getdb($sql);
            }
        }
        $tpl->set("err", $errall);
        $myamount = GetAmountSetting(1, $gametype, 0);
    }
}
$tbdList01 = "";
$tbdList02 = "";
$total = 0;
$tbdList = "";
foreach ($g_betname_3or5 as $key => $value)
{
    $updatedt = $myamount[$key]['updatedt'] == "0000-00-00 00:00:00" ? "" : date("m-d H:i", strtotime($myamount[$key]['updatedt']));
    if ($updatedt == "")
    {
        $updateid = "";
    }
    else
    {
        $updateid = $myamount[$key]['account'] == G_SUPERACCOUNT ? "─&nbsp;操盘" : "─&nbsp;" . $myamount[$key]['account'];
    }
    $tbdList = "<tr class=\"td2\">" . "<th class=\"bgClc_{$gametype}\" align=\"right\">{$value}</th>" . "<td><input size=\"6\" maxlength=\"6\" onkeypress=\"return OnlyFloat()\" class=\"tmpBack1\" name=\"back{$key}1\" value=\"" . round(100 - $myamount[$key]['back1'], 2) . "\"></td>" . "<td><input size=\"6\" maxlength=\"6\" onkeypress=\"return OnlyFloat()\" class=\"tmpBack2\" name=\"back{$key}2\" value=\"" . round(100 - $myamount[$key]['back2'], 2) . "\"></td>" . "<td><input size=\"6\" maxlength=\"6\" onkeypress=\"return OnlyFloat()\" class=\"tmpBack3\" name=\"back{$key}3\" value=\"" . round(100 - $myamount[$key]['back3'], 2) . "\"></td>" . "<td><input size=\"6\" maxlength=\"6\" onkeypress=\"return OnlyFloat()\" class=\"tmpBack4\" name=\"back{$key}4\" value=\"" . round(100 - $myamount[$key]['back4'], 2) . "\"></td>" . "<td><input size=\"6\" maxlength=\"11\" onkeypress=\"return OnlyInt()\" class=\"tmpAmount1\" name=\"amount1{$key}\" value=\"{$myamount[$key][1]}\"></td>" . "<td><input size=\"6\" maxlength=\"11\" onkeypress=\"return OnlyInt()\" class=\"tmpAmount2\" name=\"amount2{$key}\" value=\"{$myamount[$key][2]}\"></td>" . "<td class=\"bgClc_{$gametype}\">&nbsp;{$updatedt}&nbsp;{$updateid}</td>" . "</tr>";
    if (in_array($key, $listArr01))
    {
        $tbdList01 .= $tbdList;
    }
    else
    {
        $tbdList02 .= $tbdList;
    }
}
$tpl->load("main", "amount1_3d.htm");
$jslink = "<script language=\"JScript.Encode\" src=\"site.js\"></script>" . ( "<script language=\"VBScript.Encode\" src=\"site.vbs\"></script>" ) . "<script src=\"core/tpl/k2/jquery-1.4.2.min.js\"></script>";
$ui->set("jslink", $jslink);
$password_link = "&nbsp;&nbsp;&nbsp;<input type=\"button\" value=\"修改密码\" onclick=\"javascript:password_link({$gametype});\">";
$sync_amu = "&nbsp;<input type=\"button\" value=\"检查所有帐号\" onclick=\"javascript:sync_amu();\">";
$sync_amu .= "<a class=\"tooltipsSite\" href=\"#tooltipsSite\">(说明)<span><div>【＊＊重要说明＊＊】<br />";
$sync_amu .= "(1)[检查所有帐号]之功能,任何时段都可以使用◎<br />";
$sync_amu .= "(2)按下[检查所有帐号]按钮後,[{$chName}]所有帐号、所有玩法之退水与限额如果大於基本退水限额，则会更新成与基本退水限额相同值;反之，如果基本退水限额大於所有帐号之原本的基本退水限额值，则不会更新，即保留原值◎<br />";
$sync_amu .= "(3)检查帐号退水限额後,请务必人工检查{$chName}更新後的资料是否己同步◎</div></span></a>";
if ($g_init_mode_3d)
{
    $getOptionList = "玩法切换&nbsp;：&nbsp;" . getOptionList($gametype, "op.php?op=admin&fp=amount1_3d") . "&nbsp;&nbsp;&nbsp;│";
}
else
{
    $getOptionList = "";
}
$tpl->set(array ("tbdList1" => $tbdList01, "tbdList2" => $tbdList02, "total" => $total, "myWidth" => $myWidth, "url_reload" => "op.php?op=admin&fp=amount1_3d&gametype={$gametype}", "action" => "?op=admin&fp=amount1_3d&gametype={$gametype}", "submit" => L_OK, "gametype" => "bgClc_{$gametype}", "chName" => $chName, "othertext" => $othertext));
if ($gametype == 0)
{
    include( "admin_menu.php" );
}
else
{
    include( "admin_menu_3d.php" );
}
echo title("【" . $g_gametype_3d[$gametype]['ch'] . "】 基本退水 | 单项 | 单注 设定", $getOptionList . "&nbsp;选项:{$password_link}{$sync_amu}", $myWidth);
$tpl->show("main");
$ui->footer();
?>